package com.bdqn.controller;


import com.bdqn.dao.SpManageMapper;
import com.bdqn.pojo.*;
import com.bdqn.util.ExprotPOIUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller//配置类
public class SpManageController {
    @Resource
    SpManageMapper dao;

    @RequestMapping("/getSpList")
    @ResponseBody
    public Object getList(int index, int bid, String name, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        Object [] obj=null;
        String sql ="";
        if(bid != 0){
            sql = "phonesp where bid = "+bid;
        }else if (name != ""){
            sql = "phonesp where name like '%"+name+"%'";
        }else {
            sql = "phonesp";
        }
        obj = dao.getSpList(index,sql);
        List<SpMangePojo> list = (List<SpMangePojo>) obj[0];
        int count = ((ArrayList<Integer>)obj[1]).get(0);
        PagePojo page = new PagePojo(index,count,count%10==0?count/10:count/10+1,list);
        return page;
    }


    @RequestMapping("/getIdList")
    @ResponseBody
    public Object getIdList(String name,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        String sql = name.substring(0,name.length()-1);
        String[] id = sql .split(",");
        int[] array = new int[id.length];
        for (int i = 0; i < id.length; i++) {
            array[i] = Integer.parseInt(id[i]);
        }
        List<Integer> list1 = new ArrayList<Integer>();
        for (int i = 0; i < array.length; i++) {
            list1.add(array[i]);
        }
        List<SpMangePojo> Splist = dao.getIdList(list1);
        return Splist;
    }


    @RequestMapping("/getAllList")
    @ResponseBody
    public Object getBrand(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<SpMangePojo> list = dao.getAllList();
        return list;
    }


    @RequestMapping("/getAllListByName")
    @ResponseBody
    public Object getAllgetAllListByNameList(String name,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<SpMangePojo> list =dao.getAllListByName("%"+name+"%");
        return list;
    }

    @RequestMapping("/updateNum")
    @ResponseBody
    public Object getList(int num,int id, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        int result = 0;
        int count = dao.getInsertId(id);
        if(count>0){
            result = dao.updateNum(num,id);
        }else{
            result = dao.SpInsert(id,num);
        }
        return result;
    }

    @RequestMapping("/insertModel")
    @ResponseBody
    @CrossOrigin
    public Object getList( HttpServletResponse response,@RequestBody Modelpojo modelpojo){
        response.setHeader("Access-Control-Allow-Origin","*");
        int result = 0;
//        System.out.println(modelpojo.brand);
//        System.out.println(modelpojo.color);
        Brandpojo brandpojo = dao.getBrandName(modelpojo.brand);
        if(brandpojo==null){
            dao.insertBrand(modelpojo.brand);
            brandpojo = dao.getBrandName(modelpojo.brand);
        }
        modelpojo.setBid(brandpojo.id);
        int chongfu = dao.getChongfu(modelpojo);
        if(chongfu>0) {
            return result;
        }
        result = dao.insertModel(modelpojo);
        return result;
    }

    @RequestMapping("/delModel")
    @ResponseBody
    public Object delList(int id, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        int result = dao.delModel(id);

        return result;
    }

    @RequestMapping("/updatePrice")
    @ResponseBody
    public Object delList(int price,int id,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        int result = dao.updatePrice(price,id);
        return result;
    }

    @RequestMapping("/excelSp")
    @ResponseBody
    public void getList1(int bid,String name,HttpServletResponse response) throws IOException, InterruptedException {
        List<SpMangePojo> list =null;
        if(bid != 0){
            list = dao.getSpExcelBid(bid);
        }else if (name != ""){
            list = dao.getSpExcelName("%"+name+"%");
        }else {
            list = dao.getSpExcel();
        }
        String [] arr = {"id","brand","name","ram","gb","color","price","num"};
        String [] keys = {"id","brand","name","ram","gb","color","price","num"};
        Thread.sleep(5000L);
        ExprotPOIUtils.start_download(response,"测试文件",list,arr,keys);
    }



}
